﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Track.cs" company="VCD">
//   (C) 2010
// </copyright>
// <summary>
//   Defines the Track type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Id3stuff
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;

    using TagLib;

    internal class Track
    {
        private readonly TagLib.File track;

        public FileSystemInfo file { get; private set; }

        public Track(FileSystemInfo file)
        {
            this.file = file;

            TagLib.Id3v2.Tag.DefaultVersion = 3;
            TagLib.Id3v2.Tag.ForceDefaultVersion = true;

            try
            {
                this.track = TagLib.File.Create(file.FullName);
            }
            catch (Exception)
            {
                return;
            }

            this.Artist = this.track.Tag.Performers[0];
            this.Title = this.track.Tag.Title;
            this.Album = this.track.Tag.Album;
            this.AlbumArtist = this.track.Tag.FirstAlbumArtist;
        }

        public string Title { get; set; }

        public string Artist { get; set; }

        public string Album { get; set; }

        public string AlbumArtist { get; set; }

        public Image CoverArt
        {
            get
            {
                if (this.track.Tag.Pictures.Length == 0)
                {
                    return null;
                }

                var bytes = this.track.Tag.Pictures[0].Data.Data;
                return Image.FromStream(new MemoryStream(bytes, false));
            }

            set
            {
                if (value == null)
                {
                    var emptypictFrame = new IPicture[] { };
                    this.track.Tag.Pictures = emptypictFrame;
                    this.track.Save();
                    return;
                }

                string path = Path.GetDirectoryName(this.file.FullName);

                var artFileName = Path.GetTempFileName();

                value.Save(artFileName, ImageFormat.Jpeg);
                var picture = new Picture(artFileName);
                if (!System.IO.File.Exists(path + @"\folder.jpg"))
                {
                    System.IO.File.Move(artFileName, path + @"\folder.jpg");
                }
                else
                {
                    System.IO.File.Delete(artFileName);
                }

                var albumCoverPictFrame = new TagLib.Id3v2.AttachedPictureFrame(picture)
                    {
                        MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg,
                        Type = PictureType.Other
                    };
                var pictFrames = new IPicture[] { albumCoverPictFrame };
                this.track.Tag.Pictures = pictFrames;
                this.track.Save();
            }
        }
    }
}
